package cn.wolfcode.trip.admin.controller;

import cn.wolfcode.trip.base.domain.StrategyContent;
import cn.wolfcode.trip.base.domain.StrategyDetail;
import cn.wolfcode.trip.base.query.StrategyDetailQueryObject;
import cn.wolfcode.trip.base.service.IStrategyDetailService;
import cn.wolfcode.trip.base.service.IStrategyService;
import cn.wolfcode.trip.base.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 攻略文章
 */
@Controller
@RequestMapping("/strategyDetail")
public class StrategyDetailController {

    @Autowired
    private IStrategyDetailService strategyDetailService;

    @Autowired
    private IStrategyService strategyService;

    /**
     * 展示攻略文章
     * @param qo
     * @param model
     * @return
     */
    @RequestMapping("/list")
    public String list(@ModelAttribute("qo") StrategyDetailQueryObject qo, Model model){
        qo.setOrderBy("sd.sequence asc");
        model.addAttribute("pageInfo",strategyDetailService.query(qo));
        model.addAttribute("strategies",strategyService.listAll());
        return "strategyDetail/list";
    }

    /**
     * 获取攻略文章内容
     * @param id
     * @return
     */
    @RequestMapping("/getContentById")
    @ResponseBody
    public StrategyContent getContentById(Long id){
        return strategyDetailService.getContentById(id);
    }

    /**
     * 攻略文章的新增与保存
     * @param strategyDetail
     * @return
     */
    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public JsonResult saveOrUpdate(StrategyDetail strategyDetail) {
        JsonResult jsonResult = new JsonResult();

        try {
            if (strategyDetail.getId() == null) {
                strategyDetailService.save(strategyDetail);
            }else{
                strategyDetailService.update(strategyDetail);
            }
        } catch (Exception e) {
            e.printStackTrace();
            jsonResult.mark("操作异常");
        }
        return jsonResult;
    }


}
